// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// ElementUI
import '@/components/useElementUI'
import 'element-ui/lib/theme-chalk/index.css'

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import VueQr from 'vue-qr'
import 'normalize.css/normalize.css'
// 图标
import './static/icon/icons/index.js'
import Icon from './static/icon/components/Icon'
import '@/assets/iconfont/iconfont'
import Vueawesome from 'vue-awesome'
// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 全局状态
import common from '@/utils/common'
import fetch from '@/utils/fetch'
import apiType from '@/utils/apiType'
import myFilter from '@/utils/myFilter'
import constant from '@/utils/constant'
// import permission from '@/store/modules/permission'
import VueClipboard from 'vue-clipboard2'
Vue.config.productionTip = false
Vue.component('icon', Icon)
Vue.use(Vueawesome)
Vue.use(VueQr)
Vue.use(VueClipboard)

router.beforeEach((to, from, next) => {
  console.log('路由变化:' + from.name + ' -> ' + to.name)
  NProgress.start()
  store.dispatch('thirdPartyLogin', Vue.prototype.getQueryObject()).then((data) => {
    Vue.prototype.setLocalStorage('token', data.accessToken)
    Vue.prototype.setLocalStorage('name', data.name)
    Vue.prototype.setLocalStorage('operator', data.customerNumber)
    Vue.prototype.setLocalStorage('refreshToken', data.refreshToken)
    const urlArray = window.location.href.split('#')
    if (urlArray.length > 1) {
      if (urlArray[0].indexOf('?') !== -1) {
        const url = urlArray[0].substring(0, urlArray[0].indexOf('?')) + '#'
        window.location.href = (url + urlArray[1])
      }
    }
  }).catch(() => {
    const name = Vue.prototype.getLocalStorage('operator')
    if (name) {
      if (to.path === '/login') {
        next({path: '/'})
        NProgress.done() // router在hash模式下 手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps：history模式下无问题，可删除该行！
      } else {
        if (store.getters.addRouters.length > 0) { // 判断当前用户是否已拉取完user_info信息
          next()
        } else {
          console.log('获取权限树')
          store.dispatch('getRoutes').then(() => {
            router.addRoutes(store.getters.addRouters)
            next({...to}) // hack方法 确保addRoutes已完成
          }).catch(() => {
            next('/login')
          })
        }
      }
    } else {
      if (to.path === '/login') {
        next()
      } else if (to.path === '/changePwd') {
        next()
      } else if (to.path === '/404') {
        next()
      } else {
        console.log('没有登录')
        next('/login')
        NProgress.done()
      }
    }
  })
})

router.afterEach(() => {
  NProgress.done()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {App, common, fetch, apiType, myFilter, constant},
  template: '<App/>'
})
